package com.gismore.jsonp.core;

import com.google.common.collect.Maps;
import com.google.common.io.LineProcessor;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.List;
import java.util.Map;


@Log4j2
public class TargetPlaylistProcessor implements LineProcessor<Map<String, SongVO>> {

	private Map<String, SongVO> retMap = Maps.newHashMap();

	@Override
	public boolean processLine(String line) throws IOException {
		line = line.trim();
		if(line.startsWith("#")){
			return true;
		}
		SongVO songVO = new SongVO();

		String fileName = null;
		String albumTitle = null;

		String[] tagArr = line.split("\\\\");
		switch (tagArr.length){
			case 1:
				//周杰伦-青花瓷.ape
				fileName = tagArr[0];
				albumTitle = "";
				break;
			case 2:
				//周杰伦\周杰伦-青花瓷.ape
				fileName = tagArr[1];
				albumTitle = "";
				break;
			case 3:
				//Eminem\Curtain Call The Hits (Deluxe Version)\Eminem - Lose Yourself.flac
				fileName = tagArr[2];
				albumTitle = tagArr[1];
				break;
			default:
				log.error("error line : {}", line);
				return true;
		}
		songVO.setFileName(fileName);
		songVO.setAlbumTitle(albumTitle);
		setArtistAndTitle(fileName, songVO);
		songVO.setFilePath(line);

		retMap.put(fileName, songVO);
		return true;
	}

	private void setArtistAndTitle(String fileName, SongVO songVO) {
		if(fileName.contains("-")){
			String[] titleArr = fileName.split("-");
			songVO.setArtistName(titleArr[0].trim());
			songVO.setTrackTitle(StringUtils.substringBeforeLast(titleArr[1],".").trim());
		}
	}

	@Override
	public Map<String, SongVO> getResult() {
		return retMap;
	}
}

/*
周杰伦\周杰伦-青花瓷.ape
Eminem\Curtain Call The Hits (Deluxe Version)\Eminem - Lose Yourself.flac
 */
